package parse_and_index;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.pdfbox.exceptions.CryptographyException;
import org.apache.pdfbox.pdmodel.encryption.BadSecurityHandlerException;

import parse_and_index.build.inverted.BuildInverted;
import parse_and_index.build.inverted.InvertedIndex;
import parse_and_index.build.lucene.IndexFiles;
import parse_and_index.model.Document;
import parse_and_index.pdf.PDFTextExtractor;
import parse_and_index.search.inverted.Query;
import parse_and_index.search.inverted.Result;
import parse_and_index.search.inverted.SearchInverted;
import parse_and_index.search.lucene.SearchFiles;

class PDFIndexApp {

	  public enum RunMode {
			INVERTED_SEARCH, INVERTED_SEARCH_TEST, INVERTED_BUILD, LUCENE_SEARCH, LUCENE_BUILD, PDFBOX_EXTRACT
	  }

	  private static RunMode mRunMode;
	  static boolean		 test = false;

	  public static void main(String args[]) {
			try {
				  if ((args != null) && (args.length > 0)) {
						mRunMode = RunMode.valueOf(args[0]);
						args = Util.cutFirstArgFromCmdLineArgs(args);
				  }
				  switch (mRunMode) {
						case INVERTED_SEARCH :
							  SearchInverted.run(args, false);
							  break;
						case INVERTED_SEARCH_TEST :
							  SearchInverted.run(args, true);
							  break;
						case INVERTED_BUILD :
							  BuildInverted.runInvertedBuild(args);
							  break;
						case LUCENE_SEARCH :
							  SearchFiles.initSearch(args);
						case LUCENE_BUILD :
							  IndexFiles.initBuild(args);
							  break;
						case PDFBOX_EXTRACT :
							  PDFTextExtractor.extract(args);
							  break;
						default :
							  Util.printUsage();
							  break;
				  }
			}
			catch (final IllegalArgumentException e) {
				  if (Util.isValidPDFFilename(args[0])) {
						addPdfFileToIndex(args[0]);
				  }
				  else {
						searchIndex(args);
				  }
			}
			catch (final Exception e) {
				  e.printStackTrace();
				  Util.printUsage();
				  return;
			}
	  }

	  private static void addPdfFileToIndex(final String filename) {
			String outputFile;
			try {
				  outputFile = PDFTextExtractor.getOutputFile(filename, Util.DEFAULT_ENCODING);
				  PDFTextExtractor.extractToPlainText(Util.DEFAULT_ENCODING, outputFile);
				  final String docName = filename.substring(0, filename.length() - 4);
				  final Document document = new Document(docName, Util.DEFAULT_ENCODING);
				  InvertedIndex.getIndex().addDocument(outputFile, document);
			}
			catch (final UnsupportedEncodingException e) {
				  e.printStackTrace();
			}
			catch (final FileNotFoundException e) {
				  e.printStackTrace();
			}
			catch (final IOException e) {
				  e.printStackTrace();
			}
			catch (final BadSecurityHandlerException e) {
				  e.printStackTrace();
			}
			catch (final CryptographyException e) {
				  e.printStackTrace();
			}
	  }

	  private static void searchIndex(final String[] args) {
			String queryString = args[0];
			if (args.length > 1) {
				  final StringBuffer stringBuffer = new StringBuffer();
				  for (final String a : args) {
						stringBuffer.append(a);
						stringBuffer.append(" ");
				  }
				  queryString = stringBuffer.toString();
			}
			final Query query = new Query(queryString);
			final ArrayList<Result> results = InvertedIndex.getIndex().search(query);
			for (final Result result : results) {
				  System.out.println(result);
			}
	  }
}